<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/15
 * Time: 15:43
 */

namespace app\home\controller;


use app\common\controller\Backend;
use think\Db;

/**
 * @title 发布版本页面
 * @description 接口说明
 * @author jyh
 * @method POST
 */
class IssueVersions extends Backend
{
    protected $hsModel = 'IssueVersions';

    /**
     * @title 获取发布版本数据
     * @description 接口说明
     * @author jyh
     * @url issueVersions/getLst
     * @method POST
     */
    public function getLst()
    {

        $page = (int)input('page', 1);
        $limit = (int)input('limit', 10);
        $action = input('action', '');
        $data = Db::name($this->hsModel)
            ->where('deleted',null)
            ->order('ctime','desc');
        if ($action == 'all') {
            $data = $data->select();
        } else {
            $data = $data->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ], false);
        }
        return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }

    /**
     * @title 获取发布版本项目数据
     * @description 接口说明
     * @author jyh
     * @url issueVersions/getProjectLst
     * @method POST
     */
    public function getProjectLst()
    {
        $this->hsModel = 'ProjectNeed';
        // 有$map条件 就是筛选
        $where = [];
        $like = '';
        $page = (int)input('page', 1);
        $limit = (int)input('limit', 10);
        $map = $this->request->post();
        foreach ($map as $key => $value) {
            if ($value === '') unset($map[$key]);
        }
        if (isset($map['product_id'])) {
            $where['pn.product_id'] = $map['product_id'];
            if (isset($map['versions_id'])) $where['pn.versions_id'] =  $map['versions_id'];
        }
        $where['task_allot'] = ['in',[0,4]];
        $where['issue_id'] = isset($where['issue_id']) ? $map['issue_id'] : Db::name('IssueVersions')->order('ctime','desc')->value('id');
        if (isset($map['project_id'])) $where['pn.project_id'] =  $map['project_id'];
        if (isset($map['status'])) $where['pn.status'] =  $map['status'];
        if (isset($map['type'])) $where['pn.type'] = $map['type'];
        if (isset($map['priority'])) $where['pn.priority'] = $map['priority'];
        if (isset($map['front_user'])) $where['pn.front_user'] = $map['front_user'];
        if (isset($map['after_user'])) $where['pn.after_user'] = $map['after_user'];
        if (isset($map['keywords'])) $like =  "pn.id like '%{$map['keywords']}%' OR pn.name like '%{$map['keywords']}%' OR pn.describe like '%{$map['keywords']}%' OR p.name like '%{$map['keywords']}%'";
        $field = 'pn.`id`,pn.`status`,pn.`issue_id`,pn.`type`,pn.`priority`,pn.`front_user`,pn.`after_user`,pn.`product_id`,pn.`source`,pn.`describe`,pn.`creator`,pn.`name`,pn.`ctime` as `ptime`,pn.`front_predict`,pn.`after_predict`,pn.`front_plan`,pn.`after_plan`,pn.`isup`,pn.`front_adjust`,pn.`after_adjust`,pn.`block_id`,pn.`etime`';
        $data = $this->model()
            ->alias('pn')
            ->join('Project p','pn.product_id = p.id','left')
            ->where($where)
            ->where('pn.deleted','null')
            ->where($like)
            ->field($field)
            ->order('pn.ctime', 'desc')
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ], false);
        if (USER_DEPT == 0) {
            $prefix = 'front_';
        } else if (USER_DEPT == 1) {
            $prefix = 'after_';
        }
        if (isset($prefix)) {
            foreach ($data as $key => $value) {
                $data[$key]['isshow'] = $value[$prefix.'predict'] > 0 ? false : true;
                $data[$key]['isself'] = $value[$prefix.'user'] == USER_NAME ? true : false;
                $data[$key]['isfinish'] = $value[$prefix.'plan'] == 100 ? false : true;
            }
        }
        return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }

    /**
     * @title 新增发布版本
     * @description 接口说明
     * @author jyh
     * @url issueVersions/add
     * @param name:name type:string require:1 default: other: desc:发布版本名称
     * @param name:ishalt type:int require:1 default: other: desc:是否停机
     * @param name:describe type:string require:1 default: other: desc:停机公告描述
     * @param name:uptime type:date require:1 default: other: desc:发布时间日期 (年月日)
     * @method POST
     */
    public function add()
    {
        return parent::add(); // TODO: Change the autogenerated stub
    }

    /**
     * @title 删除发布版本
     * @description 接口说明
     * @author jyh
     * @url issueVersions/del/:id
     * @method POST
     * @param name:ids type:int require:1 default: other: desc:需求ids
     */
    public function del($ids = "")
    {
        return parent::del($ids); // TODO: Change the autogenerated stub
    }
}